#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Wumpus game level (cave) generator and validation tool.

author: Mikolaj Podlaszewski <mikolaj.podlaszewski@uni.lu>
date: 07 Nov 2012
"""

import argparse

parser = argparse.ArgumentParser(description='Generates and validates caves inhabited by Wumpus.')
parser.add_argument('--wumpus', '-w', action='store_true',
                   help='add wumpus')
parser.add_argument('--pits', '-p', action='store_true',
                   help='add pits')
parser.add_argument('--bats', '-b', action='store_true',
                   help='add bats')
parser.add_argument('--shape', choices=['rect'],
                  default='rect',
                   help='shape of the cave')
parser.add_argument('--size', type=int,
                   default=100,
                   help='size of the cave')
parser.add_argument('cave', type=argparse.FileType('r'), nargs='?',
                   help='file with cave map to validate')

args = parser.parse_args()

from cave import Cave


if args.cave:
    c = Cave(args.cave.read())
    e, w = c.validate()
    if e:
        print 'Errors:'
        for m in e: print '\t' + m
    if w:
        print 'Warnings:'
        for m in w: print '\t' + m
    if not e and not w:
        print 'Ok'
else:
    from random import *

    c = Cave()
    # generate cave
    if args.shape == 'rect':
        rto = random()
        w = int((args.size*(rto**-1))**0.5)
        h = int(rto*w)
        
        for x in range(w):
            for y in range(h):
                c[x,y] = set()

    rooms = [r for r in c.rooms()]

    # add entrance
    c[choice(rooms)].add('E')

    #add gold
    c[choice(rooms)].add('G')

    #add wumpus
    if args.wumpus:
        c[choice(rooms)].add('W')

    if args.pits:
        for i in range(1+int(random() * 0.2 * args.size)):
            c[choice(rooms)].add('P')
    
    print c

